# -*- coding: utf-8 -*-
"""
@Time    : 2021/10/9 17:46
@Author  : Leibuyun
@File    : 1.14 排序不支持原生比较的对象.py
"""

from operator import attrgetter


class User:
    def __init__(self, user_id):
        self.user_id = user_id

    def __repr__(self):
        return f'User({self.user_id})'


if __name__ == '__main__':
    users = [User(23), User(205), User(2)]
    """
    sorted() 函数有一个关键字参数key, 可以传入一个callable对象给它， 
    对每个传入的对象返回一个值，这个值被sorted用来排序这些对象
    """
    print(sorted(users, key=attrgetter('user_id')))
    print(sorted(users, key=lambda user: user['user_id']))
